package com.stgl.service;

import com.stgl.bean.Template;
import com.stgl.bean.TemplateParam;
import com.stgl.dao.StudentDao;
import com.stgl.dao.WechartDao;
import com.stgl.util.ConfKit;
import com.stgl.wxutil.AdvancedUtil;
import com.stgl.wxutil.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class StudentService {
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private WechartDao wechartDao;

    public List<Map<String,Object>>Search_Student(Map<String,Object>gmap)
    {
        List<Map<String,Object>>list1=studentDao.Search_Student(gmap);
        for(int i=0;i<list1.size();i++){
            Map<String,Object>smap=list1.get(i);
            //查询相关图片
            Map<String,Object>simage=new HashMap<>();
            simage.put("tablename","student");
            simage.put("zjid",smap.get("SID"));
            simage.put("zdmc","hjszd");
            List<Map<String,Object>>list2=wechartDao.getListImageUrl(simage);
            smap.put("imgurls",list2);
        }
        return list1;
    }


    public List<Map<String,Object>>Search_Student_page(Map<String,Object>gmap)
    {
        List<Map<String,Object>>list1=studentDao.Search_Student_page(gmap);
        for(int i=0;i<list1.size();i++){
            Map<String,Object>smap=list1.get(i);
            //查询相关图片
            Map<String,Object>simage=new HashMap<>();
            simage.put("tablename","student");
            simage.put("zjid",smap.get("SID"));
            simage.put("zdmc","hjszd");
            List<Map<String,Object>>list2=wechartDao.getListImageUrl(simage);
            smap.put("imgurls",list2);
        }
        return list1;
    }


    public int Search_Student_count(Map<String,Object>gmap)
    {
        int countall=studentDao.Search_Student_count(gmap);
        return countall;
    }


    public Map<String,Object> del_Student(Map<String,Object>gmap)
    {
        Map<String,Object>smap=new HashMap<>();
        //查询学生是否已经报名成功
        List<Map<String,Object>>list1=studentDao.Search_Student(gmap);
        if(list1.size()>0){
            String sfbmcg=list1.get(0).get("SFBMCG").toString();
            if(sfbmcg.equals("1")){
                smap.put("result",false);
                smap.put("msg","该学生已经报名成功,不能删除!");
            }else{
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                gmap.put("scsj",df.format(new Date()));
                int i=studentDao.del_Student(gmap);
                if(i>0){
                    smap.put("result",true);
                    smap.put("msg","删除成功");
                }else{
                    smap.put("result",false);
                    smap.put("msg","删除失败");
                }
            }
        }
        return smap;
    }




    public int update_Student(Map<String,Object>gmap)
    {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        gmap.put("bmcgsj",df.format(new Date()));
        return studentDao.update_Student(gmap);
    }




    public boolean update_StudentJg(Map<String,Object>gmap)
    {
        //56ce1a350a7f4b3c8fe7514b8377ffcf
        int i = studentDao.update_StudentJg(gmap);
       /*
        ConfKit confkit= new ConfKit("wxpay.properties");
        //查询所有学生
        List<Map<String,Object>>lisall=studentDao.getall_Student(gmap);
        for(int j=0;j<lisall.size();j++){
            Map<String,Object>maps=lisall.get(j);
            if(maps.get("SFBMCG")!=null){
                //发送消息
                String bmsj=maps.get("BMSJ").toString();
                String sname=maps.get("SNAME").toString();
                String sfzh=maps.get("SFZH").toString();
                String csname=maps.get("CSNAME").toString();
                String bmjg="";
                if(maps.get("SFBMCG").toString().equals("0")){
                    //报名失败的人
                     bmjg="很遗憾,报名失败";
                }else if(maps.get("SFBMCG").toString().equals("1")){
                     bmjg="恭喜您,报名成功";
                }

                String accessToken = CommonUtil.getToken(confkit.get("appid"),confkit.get("appsecret")).getAccessToken();
                List<TemplateParam> tpList=new ArrayList<TemplateParam>();
                tpList.add(new TemplateParam("keyword1",bmsj,""));
                tpList.add(new TemplateParam("keyword2",bmjg,""));
                tpList.add(new TemplateParam("keyword3",sname,""));
                tpList.add(new TemplateParam("keyword4",sfzh,""));
                tpList.add(new TemplateParam("keyword5",csname,""));

                Template template=new Template();//模板对象
                template.setTemplateId("3nAedc88zLhcr0hkYKKlwgHiVWUfCAuqfs7Oz2PT_14");//模板Id
                //发给谁
                String opendid=maps.get("WXID").toString();
                template.setToUser(opendid);
                template.setTopColor("#FF0000");
                template.setTemplateParamList(tpList);
                AdvancedUtil.sendTemplateMessage(accessToken,template);
                //发送消息结束
            }
        }*/
        return true;
    }

    public boolean set_sfbmcg(Map<String,Object>gmap) {
        int i = studentDao.set_sfbmcg(gmap);
        if(i>0){
            return true;
        }else{
            return false;
        }
    }


    public int Insert_stufile(Map<String,Object>gmap) {
        return studentDao.Insert_stufile(gmap);
    }


    public int del_stufile(Map<String,Object>gmap) {
        return studentDao.del_stufile(gmap);
    }


    public boolean ccsh_Student(Map<String,Object>gmap) {
        int i = studentDao.ccsh_Student(gmap);
        if(i>0){
            return true;
        }else{
            return false;
        }
    }



    //@Scheduled(fixedRate =1800000) //： 表示 每隔 5000 毫秒执行一次
    public void reportCurrentTime() {
        System.out.println("每隔五秒钟执行一次： " + new Date());
    }

}


